/*
 * Copyright (c) 2017, www.zm0618.com All Rights Reserved..
 */

package com.light.hessian.remoting.client.xml;

import com.light.hessian.remoting.client.balance.Balance;
import com.light.hessian.remoting.client.core.HessianClientProxyFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

/**
 * Created by zhaojun on 2017/02/08.
 */
public class HessianClientBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{

    @Override
    protected Class<?> getBeanClass(Element element) {
        return HessianClientProxyFactory.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {

        String serviceName=element.getAttribute("serviceName");
        String serviceInterface=element.getAttribute("serviceInterface");
        if (StringUtils.isBlank(serviceName)){
            String serviceInterfaceName=serviceInterface.substring(serviceInterface.lastIndexOf(".")+1);
            serviceName=serviceInterfaceName.substring(0,1).toLowerCase()+serviceInterfaceName.substring(1);
        }
        builder.addPropertyValue("serviceName",serviceName);
        builder.addPropertyValue("serviceInterface",serviceInterface);

        String reference= element.getAttribute("reference");
        builder.addPropertyValue("reference",reference);

        String id=element.getAttribute("id");
        if (StringUtils.isBlank(id)){
            builder.addPropertyValue("id",serviceName);
        }
        String balance=element.getAttribute("balance");
        if (StringUtils.isNotBlank(balance)){
            builder.addPropertyValue("balance", Balance.valueOf(balance));
        }
    }
}
